package demon2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {
    private Socket socket=null;
    public TcpClient(String SeverIP,int SeverPort) throws IOException {
        socket=new Socket(SeverIP,SeverPort);
    }
    public void start(){
        System.out.println("客户端使用");
        Scanner scanner =new Scanner(System.in);

        try(InputStream inputStream=socket.getInputStream();
            OutputStream outputStream=socket.getOutputStream()) {
            Scanner scannernet=new Scanner(inputStream);
            while(true){
                System.out.println("请输入要发送的内容");
                String msg=scanner.next();
                msg+="\n";
                outputStream.write(msg.getBytes());
                if(!scannernet.hasNext()){
                    break;
                }
                String respon=scannernet.next();
                System.out.println(respon);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpClient tcpClient=new TcpClient("127.0.0.1",9090);
        tcpClient.start();
    }
}
